import 'package:check_app/page/SubjectDetailPage.dart';
import 'package:check_app/page/SubjectInputPage.dart';
import 'package:flutter/material.dart';
import 'package:check_app/widgets/SearchBar.dart';
import 'package:check_app/widgets/SubjectList.dart';
import 'package:check_app/dao/subject_dao.dart';
import 'package:fluttertoast/fluttertoast.dart';

const PATH = '/app/subject/list';

class SubjectPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => new _SubjectPageState();
}

class _SubjectPageState extends State<SubjectPage> {
  bool _loading = false;
  String keyword = '';
  int pageNum = 1;
  int pageSize = 10;
  int total = 0;
  List subjectList = [];

  @override
  void initState() {
    super.initState();
    getData(PATH);
  }

  Future<Null> getData(String path) async {
    setState(() {
      _loading = true;
    });
    try {
      var result = await SubjectDao.fetchSubjectList(path, {
        'keyword': keyword,
        'pageNum': pageNum,
        'pageSize': pageSize,
      });
      setState(() {
        _loading = false;
      });
      if (result.success) {
        total = result.total;
        setState(() {
          subjectList.addAll(result.data);
        });
      } else {
        Fluttertoast.showToast(
          msg: result.message,
        );
      }
    } catch (e) {
      print(e);
      setState(() {
        _loading = false;
      });
    }
    return null;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Color.fromRGBO(248, 248, 249, 1),
      body: SafeArea(
        child: Column(
          children: <Widget>[
            Container(
              padding: EdgeInsets.only(bottom: 8),
              decoration: BoxDecoration(
                color: Color.fromRGBO(255, 255, 255, 1),
                border: Border(
                  bottom: BorderSide(
                    width: 0.5,
                    color: Color.fromRGBO(229, 229, 229, 1),
                  ),
                ),
              ),
              child: SearchBar(
                  placeHolder: "请输入关键词搜索",
                  searchCb: (text) {
                    keyword = text;
                    pageNum = 1;
                    total = 0;
                    subjectList = [];
                    getData(PATH);
                  }
              ),
            ),
            Expanded(
              child: SubjectList(
                total: total,
                list: subjectList,
                upRefresh: () {
                  pageNum += 1;
                  getData(PATH);
                },
                onclick: (item) {
                  if (item.generateStatus == 'YES') {
                    Navigator.of(context).push(MaterialPageRoute(builder: (_) {
                      return SubjectDetailPage(id: item.id, subName: item.subName);
                    }));
                  } else if (item.generateStatus == 'NO') {
                    Navigator.of(context).push(MaterialPageRoute(builder: (_) {
                      return SubjectInputPage(id: item.id, subName: item.subName);
                    }));
                  }
                },
              ),
            ),
          ],
        ),
      ),
    );
  }
}
